/**
 * @ author: 汪孟苹
 */
define(["jquery", "bbcare"], function ($, $bb) {
    var note = {};
    
    /**
     * 校验规则
     */
    var noteNoNull = function(params){              // 非空
        var _default = {
            "ruleVal":true,
        };
        params = $.extend(_default,params);
        if( params.obj.attr("type") == "radio" || params.obj.attr("type") == "checkbox" ){
            var success = params.value > 0 ? true : false;
        }else{
            var reg = /\S/;
            var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        }
        return success;
    }
    var noteNum = function(params){              // 整数+小数
        var _default = {
            "ruleVal":true,
        };
        params = $.extend(_default,params);
        var reg = /^$|^-?\d+(\.\d+)?$/;
        var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        return success;
    }
    var noteMoreThanNum = function(params){     // 大于一个数   
        var _default = {
            "ruleVal":0,
        };
        params = $.extend(_default,params);          
        if( params.value == "" ) return true;
        var success = Number(params.value) > Number(params.ruleVal) ? true : false;
        return success;
    }
    var noteLessThanNum = function(params){     // 小于一个数   
        var _default = {
            "ruleVal":0,
        };
        params = $.extend(_default,params);          
        if( params.value == "" ) return true;
        var success = Number(params.value) < Number(params.ruleVal) ? true : false;
        return success;
    }
    
    /**
     * 校验处理
     */
    var noteHandle = function(obj){
        var noteJson = obj.attr("data-note");
        if(!noteJson || noteJson.trim().length == 0) return true;
    	var options = JSON.parse( noteJson );
		var value = obj.val().trim(); // 获取输入的值
        if( typeof(obj.attr("type")) != "undefined" ){
            var type = obj.attr("type");
            if( type == "radio" ){
                value = obj.parents(".radio").find("input[type=radio]:checked").length;
            }else if( type == "checkbox" ){
                value = obj.parents(".checkbox").find("input[type=checkbox]:checked").length;
            }
        }
		if(!options || options.length == 0) return true;
        var result = true;
        for (var i = 0; i < options.length; i++) {
            var params = {
                "value":value,
                "ruleVal":options[i].val,
                "obj":obj,
            }
            switch(options[i].rule) {
                case "noNull":
                    result = result && noteNoNull(params);
                    break;
                case "num":
                    result = result && noteNum(params);
                    break;
                case "moreThanNum":
                    result = result && noteMoreThanNum(params);
                    break;
                case "lessThanNum":
                    result = result && noteLessThanNum(params);
                    break;
            }
        }
        return result;
    }

    /**
     * 没通过校验的提示信息
     */
    var noteTip = function(success,obj){
        if(!success){
            obj.parents(".qaCard").addClass("has-warning");
        }else{
            obj.parents(".qaCard").removeClass("has-warning");
        }
    }
    
    /**
     * 失去焦点时触发
     */
    note.blurInit = function(obj){
        var success = noteHandle(obj);
        noteTip(success,obj);
    }

    return note;
});